package carouselMap

import (
	"context"
	"math"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/carouselmap"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CarouselMapListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 列表
func NewCarouselMapListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CarouselMapListLogic {
	return &CarouselMapListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CarouselMapListLogic) CarouselMapList(req *types.CarouselMapListReq) (resp *types.CarouselMapListDataRes, err error) {
	resData := make([]types.CarouselMapListItemRes, 0)
	resp = &types.CarouselMapListDataRes{
		List: resData,
		Pagination: types.Pagination{
			PageSize:  int32(req.Size),
			Page:      int32(req.Page),
			Total:     0,
			TotalPage: 0,
		},
	}

	u, err := l.svcCtx.SettingService.CarouselMap.CarouselMapList(l.ctx, &carouselmap.CarouselMapListReq{Page: req.Page, Size: req.Size, MerchantId: 1})

	if err != nil {
		logc.Error(l.ctx, "CarouselMapList err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	for _, v := range u.Data.List {
		resData = append(resData, types.CarouselMapListItemRes{
			MerchantId: v.MerchantId,
			Id:         v.Id,
			Pic:        v.Pic,
			JumpUrl:    v.JumpUrl,
			Sort:       v.Sort,
			Status:     int64(v.Status),
		})
	}

	resp.Pagination.Total = u.Data.Count
	resp.Pagination.TotalPage = int64(math.Ceil(float64(u.Data.Count) / float64(req.Size)))
	resp.List = resData
	return resp, nil
}
